﻿package mortal.game.manager
{
    import com.greensock.*;
    import com.mui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.scene3D.map3D.*;
    import mortal.game.view.common.*;
    import mortal.game.view.mainUI.actionIcon.*;
    import mortal.mvc.core.*;

    public class ActionIconLayer extends View implements ILayer
    {
        private var _iconSpeArr:Array;
        private var colCount:int = 10;
        private var comCount:int = 10;
        private var smallCount:int = 8;
        private const iconWidth:int = 65;
        private const iconHeight:int = 65;
        private var vcDisplaObject:Array;
        private var spVcDisplaObject:Array;
        private var _commonLineArr:Array;
        private var _specialLineArr:Array;
        private var _comRowHideArr:Array;
        private var _speRowHideArr:Array;
        private var _iconContainer:GSprite;
        private var _btnContainer:GSprite;
        private var _iconMask:GSprite;
        public var myHeight:int = 0;
        private var _btnClose:GLoadedButton;
        private var _btnOpen:GLoadedButton;
        private var _isAllClose:Boolean = false;

        public function ActionIconLayer()
        {
            this.vcDisplaObject = [];
            this.spVcDisplaObject = [];
            this._commonLineArr = [];
            this._specialLineArr = [];
            this._comRowHideArr = [];
            this._speRowHideArr = [];
            this._iconSpeArr = ["FashionOnlineIcon", "InvestmentIcon", "WelfareIcon", "FirstReChargeIcon", ResFileConst.FirstRechargeDay500Icon, "RebateIcon", "SevenDayLoginIcon", ResFileConst.AgencyWelfareIcon, ResFileConst.LanderIcon, "OpenServerSevenIcon", "RebateDayRechargeIcon", "RebateDayTargetIcon", ResFileConst.lotteryDrawIcon, "MonthInvestmentIcon", "SubmitBugIcon", ResFileConst.NationalDayActIcon, ResFileConst.WeChatIcon, ResFileConst.YYZiZuanIcon, ResFileConst.VipIcon, ResFileConst.Comsum200Icon, ResFileConst.PlatformTencentVipIcon, ResFileConst.RechargeDailyTotalIcon, ResFileConst.ShopQQIcon, undefined, ResFileConst.ShopSecKillIcon, ResFileConst.QQBlueVipXufeiIcon, ResFileConst.ThanksGivingIcon, ""];
            tabChildren = false;
            mouseEnabled = false;
            this.createChildren();
            return;
        }// end function

        private function createChildren() : void
        {
            this._iconContainer = UIFactory.getUICompoment(GSprite);
            this._iconContainer.mouseEnabled = false;
            this._iconContainer.x = 50;
            this._iconContainer.y = -10;
            this.addChild(this._iconContainer);
            this._iconMask = UIFactory.getUICompoment(GSprite);
            this._iconMask.mouseEnabled = false;
            this._iconMask.x = -750;
            this._iconMask.y = -20;
            this.addChild(this._iconMask);
            this._iconContainer.mask = this._iconMask;
            this._btnContainer = UIFactory.getUICompoment(GSprite);
            this._btnContainer.mouseEnabled = false;
            this._btnContainer.x = 50;
            this._btnContainer.y = -10;
            this.addChild(this._btnContainer);
            this._btnOpen = UIFactory.gLoadedButton(ImagesConst.ui_mapUIOpen_upSkin, 0, 0, 21, 20);
            this._btnClose = UIFactory.gLoadedButton(ImagesConst.ui_mapUIClose_upSkin, 0, 0, 21, 20);
            this._btnOpen.visible = false;
            this._btnOpen.configEventListener(MouseEvent.CLICK, this.onShowHideHd);
            this._btnClose.configEventListener(MouseEvent.CLICK, this.onShowHideHd);
            EventUtil.addSelfEvent(EventName.ActionIconOpenClose, this.onOpenCloseHd, true);
            return;
        }// end function

        private function onOpenCloseHd(param1:Object) : void
        {
            var _loc_3:int = 0;
            var _loc_5:ActionIconOneLineSp = null;
            var _loc_2:* = param1.data as Boolean;
            this._isAllClose = !_loc_2;
            var _loc_4:* = this._commonLineArr;
            _loc_3 = 0;
            while (_loc_3 < _loc_4.length)
            {
                
                _loc_5 = _loc_4[_loc_3];
                _loc_5.isHide = !_loc_2;
                _loc_3++;
            }
            _loc_4 = this._specialLineArr;
            _loc_3 = 0;
            while (_loc_3 < _loc_4.length)
            {
                
                _loc_5 = _loc_4[_loc_3];
                _loc_5.isHide = !_loc_2;
                _loc_3++;
            }
            return;
        }// end function

        private function onShowHideHd(event:MouseEvent) : void
        {
            if (event.currentTarget == this._btnOpen)
            {
                this._btnOpen.visible = false;
                this._btnClose.visible = true;
                TweenMax.to(this._iconContainer, 0.2, {y:0});
            }
            else
            {
                this._btnClose.visible = false;
                this._btnOpen.visible = true;
                TweenMax.to(this._iconContainer, 0.2, {y:-this.myHeight});
            }
            return;
        }// end function

        private function addOneIcon(param1:Array, param2:Array, param3:DisplayObject, param4:int) : int
        {
            var _loc_5:* = param1[param4];
            if (_loc_5 == null)
            {
                var _loc_6:* = UIFactory.getUICompoment(ActionIconOneLineSp);
                param1[param4] = UIFactory.getUICompoment(ActionIconOneLineSp);
                _loc_5 = _loc_6;
                _loc_5.parentSp = this._iconContainer;
                _loc_5.colCount = this.colCount;
                _loc_5.isHide = param2[param4] == null ? (false) : (param2[param4]);
            }
            if (_loc_5.iconArr.length >= this.colCount)
            {
                param4++;
                var _loc_6:* = UIFactory.getUICompoment(ActionIconOneLineSp);
                param1[param4] = UIFactory.getUICompoment(ActionIconOneLineSp);
                _loc_5 = _loc_6;
                _loc_5.parentSp = this._iconContainer;
                _loc_5.colCount = this.colCount;
                _loc_5.isHide = param2[param4] == null ? (false) : (param2[param4]);
            }
            if (this._isAllClose)
            {
                _loc_5.isHide = this._isAllClose;
            }
            _loc_5.addIcon(param3);
            _loc_5.iconArr.sort(this.sortIcons);
            _loc_5.layOutIcons();
            return param4;
        }// end function

        private function layOutAll() : void
        {
            var _loc_1:int = 0;
            var _loc_4:ActionIconOneLineSp = null;
            var _loc_2:int = 0;
            var _loc_3:* = this._commonLineArr;
            _loc_1 = 0;
            while (_loc_1 < _loc_3.length)
            {
                
                _loc_4 = _loc_3[_loc_1];
                _loc_4.y = _loc_1 * this.iconHeight;
                _loc_4.layY();
                this._btnContainer.addChild(_loc_4);
                _loc_2++;
                _loc_1++;
            }
            _loc_3 = this._specialLineArr;
            _loc_1 = 0;
            while (_loc_1 < _loc_3.length)
            {
                
                _loc_4 = _loc_3[_loc_1];
                _loc_4.y = _loc_2 * this.iconHeight;
                _loc_4.layY();
                this._btnContainer.addChild(_loc_4);
                _loc_2++;
                _loc_1++;
            }
            this.resetChildIndex();
            this.myHeight = (_loc_2 + 1) * this.iconHeight;
            this.resizeMask();
            return;
        }// end function

        private function resetChildIndex() : void
        {
            var _loc_2:ActionIconOneLineSp = null;
            var _loc_1:* = this._commonLineArr;
            var _loc_3:int = 0;
            _loc_1 = this._specialLineArr;
            _loc_3 = _loc_1.length - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_2 = _loc_1[_loc_3];
                this._btnContainer.addChild(_loc_2);
                _loc_3 = _loc_3 - 1;
            }
            _loc_1 = this._commonLineArr;
            _loc_3 = _loc_1.length - 1;
            while (_loc_3 >= 0)
            {
                
                _loc_2 = _loc_1[_loc_3];
                this._btnContainer.addChild(_loc_2);
                _loc_3 = _loc_3 - 1;
            }
            return;
        }// end function

        private function resizeMask() : void
        {
            this._iconMask.graphics.clear();
            this._iconMask.graphics.beginFill(1);
            this._iconMask.graphics.drawRect(0, 0, 800, this.myHeight);
            this._iconMask.graphics.endFill();
            this._iconContainer.mask = this._iconMask;
            return;
        }// end function

        private function removeAll(param1:Array, param2:Array, param3:Array) : void
        {
            var _loc_5:ActionIconOneLineSp = null;
            param3.length = 0;
            var _loc_4:int = 0;
            while (_loc_4 < param2.length)
            {
                
                _loc_5 = param2[_loc_4];
                param3[_loc_4] = _loc_5.isHide;
                if (_loc_5.parent)
                {
                    _loc_5.parent.removeChild(_loc_5);
                }
                _loc_5.dispose();
                _loc_4++;
            }
            param2.length = 0;
            return;
        }// end function

        private function removeAllAndAddOne(param1:Array, param2:Array, param3:Array, param4:DisplayObject) : void
        {
            var _loc_7:DisplayObject = null;
            this.removeAll(param1, param2, param3);
            param2.length = 0;
            param1.push(param4);
            if (param1.length > 1)
            {
                param1.sort(this.sortIcons);
            }
            var _loc_5:int = 0;
            var _loc_6:int = 0;
            while (_loc_6 < param1.length)
            {
                
                _loc_7 = param1[_loc_6];
                _loc_5 = this.addOneIcon(param2, param3, _loc_7, _loc_5);
                _loc_6++;
            }
            return;
        }// end function

        private function removeOneIcon(param1:Array, param2:Array, param3:Array, param4:DisplayObject) : void
        {
            var _loc_8:DisplayObject = null;
            this.removeAll(param1, param2, param3);
            var _loc_5:* = param1.indexOf(param4);
            if (_loc_5 != -1)
            {
                param1.splice(_loc_5, 1);
            }
            var _loc_6:int = 0;
            var _loc_7:int = 0;
            while (_loc_7 < param1.length)
            {
                
                _loc_8 = param1[_loc_7];
                _loc_6 = this.addOneIcon(param2, param3, _loc_8, _loc_6);
                _loc_7++;
            }
            return;
        }// end function

        public function addPopUp(param1:DisplayObject, param2:Boolean = false) : void
        {
            var _loc_3:ActionIconOneLineSp = null;
            if (this.vcDisplaObject.indexOf(param1) != -1 || this.spVcDisplaObject.indexOf(param1) != -1)
            {
                return;
            }
            var _loc_4:* = param1 as ActionBaseIcon;
            if (_loc_4 == null)
            {
                return;
            }
            if (this._iconSpeArr.indexOf(_loc_4.btnName) != -1)
            {
                this.removeAllAndAddOne(this.spVcDisplaObject, this._specialLineArr, this._speRowHideArr, param1);
            }
            else
            {
                this.removeAllAndAddOne(this.vcDisplaObject, this._commonLineArr, this._comRowHideArr, param1);
            }
            this.layOutAll();
            return;
        }// end function

        public function removePopup(param1:DisplayObject, param2:Boolean = true) : void
        {
            var _loc_3:ActionIconOneLineSp = null;
            if (this.vcDisplaObject.indexOf(param1) == -1 && this.spVcDisplaObject.indexOf(param1) == -1)
            {
                return;
            }
            var _loc_4:* = param1 as ActionBaseIcon;
            if (_loc_4 == null)
            {
                return;
            }
            if (param1.parent)
            {
                param1.parent.removeChild(param1);
            }
            if (this._iconSpeArr.indexOf(_loc_4.btnName) != -1)
            {
                this.removeOneIcon(this.spVcDisplaObject, this._specialLineArr, this._speRowHideArr, param1);
            }
            else
            {
                this.removeOneIcon(this.vcDisplaObject, this._commonLineArr, this._comRowHideArr, param1);
            }
            this.layOutAll();
            return;
        }// end function

        private function sortIcons(param1:ActionBaseIcon, param2:ActionBaseIcon) : int
        {
            if (param1.sortId > param2.sortId)
            {
                return -1;
            }
            if (param1.sortId < param2.sortId)
            {
                return 1;
            }
            if (param1.btnName.toLocaleLowerCase() < param2.btnName.toLocaleLowerCase())
            {
                return -1;
            }
            if (param1.btnName.toLocaleLowerCase() > param2.btnName.toLocaleLowerCase())
            {
                return 1;
            }
            if (param1.name.toLocaleLowerCase() < param2.name.toLocaleLowerCase())
            {
                return -1;
            }
            if (param1.name.toLocaleLowerCase() > param2.name.toLocaleLowerCase())
            {
                return 1;
            }
            return 0;
        }// end function

        public function addPopUpFly(param1:DisplayObject, param2:Function = null) : void
        {
            var onCompleteFunc:Function;
            var displayObject:* = param1;
            var onFlyComplete:* = param2;
            onCompleteFunc = function () : void
            {
                if (onFlyComplete != null)
                {
                    onFlyComplete();
                }
                return;
            }// end function
            ;
            var pointCur:* = new Point(displayObject.x, displayObject.y);
            pointCur = displayObject.parent.localToGlobal(pointCur);
            pointCur = this.globalToLocal(pointCur);
            displayObject.x = pointCur.x;
            displayObject.y = pointCur.y;
            this._iconContainer.addChild(displayObject);
            var pointTarget:* = this.getNextPosition();
            TweenLite.to(displayObject, 0.8, {x:pointTarget.x, y:pointTarget.y, onComplete:onCompleteFunc});
            this.vcDisplaObject.push(displayObject);
            return;
        }// end function

        public function centerPopup(param1:DisplayObject) : void
        {
            return;
        }// end function

        public function setPosition(param1:DisplayObject, param2:int, param3:int) : void
        {
            return;
        }// end function

        public function isTop(param1:DisplayObject) : Boolean
        {
            if (this._iconContainer.contains(param1))
            {
                return this._iconContainer.getChildIndex(param1) == (this._iconContainer.numChildren - 1);
            }
            return false;
        }// end function

        public function isPopup(param1:DisplayObject) : Boolean
        {
            return this._iconContainer.contains(param1);
        }// end function

        public function setTop(param1:DisplayObject) : void
        {
            if (this._iconContainer.contains(param1))
            {
                if (this._iconContainer.getChildIndex(param1) != (this._iconContainer.numChildren - 1))
                {
                    this._iconContainer.setChildIndex(param1, (this._iconContainer.numChildren - 1));
                }
            }
            return;
        }// end function

        public function getNextPosition() : Point
        {
            var _loc_1:* = new Point();
            _loc_1.x = this.vcDisplaObject.length % this.colCount * (-this.iconWidth);
            _loc_1.y = int(this.vcDisplaObject.length / this.colCount) * this.iconHeight;
            return _loc_1;
        }// end function

        public function updateAllIconPositon() : void
        {
            return;
        }// end function

        private function clearDis() : void
        {
            var _loc_1:int = 0;
            var _loc_3:ActionIconOneLineSp = null;
            var _loc_2:* = this._commonLineArr;
            _loc_1 = 0;
            while (_loc_1 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_1];
                _loc_3.dispose();
                _loc_1++;
            }
            this._commonLineArr = [];
            _loc_2 = this._specialLineArr;
            _loc_1 = 0;
            while (_loc_1 < _loc_2.length)
            {
                
                _loc_3 = _loc_2[_loc_1];
                _loc_3.dispose();
                _loc_1++;
            }
            this._specialLineArr = [];
            return;
        }// end function

        private function relayout() : void
        {
            var _loc_4:DisplayObject = null;
            var _loc_5:* = [];
            this._comRowHideArr = [];
            this._speRowHideArr = _loc_5;
            var _loc_1:* = this.vcDisplaObject;
            this.clearDis();
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            _loc_3 = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_4 = _loc_1[_loc_3];
                _loc_2 = this.addOneIcon(this._commonLineArr, this._comRowHideArr, _loc_4, _loc_2);
                _loc_3++;
            }
            _loc_1 = this.spVcDisplaObject;
            _loc_2 = 0;
            _loc_3 = 0;
            while (_loc_3 < _loc_1.length)
            {
                
                _loc_4 = _loc_1[_loc_3];
                _loc_2 = this.addOneIcon(this._specialLineArr, this._speRowHideArr, _loc_4, _loc_2);
                _loc_3++;
            }
            this.layOutAll();
            return;
        }// end function

        override public function stageResize() : void
        {
            if (SceneRange.display.width <= 1366)
            {
                if (this.colCount != this.smallCount)
                {
                    this.colCount = this.smallCount;
                    this.relayout();
                }
            }
            else if (this.colCount != this.comCount)
            {
                this.colCount = this.comCount;
                this.relayout();
            }
            this.x = SceneRange.display.width - 330;
            this.y = 20;
            return;
        }// end function

    }
}
